package com.xiaoshuidi.cloud.module.system.controller.app.tenant;

import com.xiaoshuidi.cloud.framework.common.enums.CommonStatusEnum;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.system.controller.admin.tenant.vo.tenant.TenantExportReqVO;
import com.xiaoshuidi.cloud.module.system.controller.admin.tenant.vo.tenant.TenantPageReqVO;
import com.xiaoshuidi.cloud.module.system.controller.admin.tenant.vo.tenant.TenantRespVO;
import com.xiaoshuidi.cloud.module.system.convert.tenant.TenantConvert;
import com.xiaoshuidi.cloud.module.system.dal.dataobject.tenant.TenantDO;
import com.xiaoshuidi.cloud.module.system.service.tenant.TenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name =  "管理后台 - 租户")
@RestController
@RequestMapping("/system/tenant")
public class AppTenantController {

    @Resource
    private TenantService tenantService;

    @GetMapping("/page")
    @Operation(summary = "获得租户分页")
    @PermitAll
    public CommonResult<PageResult<TenantRespVO>> getTenantPage(@Valid TenantPageReqVO pageVO) {
        PageResult<TenantDO> pageResult = tenantService.getTenantPage(pageVO);
        return success(TenantConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/list-all")
    @Operation(summary = "获取租户精简信息列表", description = "只包含被开启的租户，主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('system:tenant:query')")
    public CommonResult<List<TenantRespVO>> getSimpleUsers() {
        TenantExportReqVO reqVO = new TenantExportReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<TenantDO> list = tenantService.getTenantList(reqVO);
        return success(TenantConvert.INSTANCE.convertList(list));
    }


}